
package pos.control;

import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Statement;
import pos.Pos;
import pos.storage.db.sql.SQL;
import pos.exception.POSException;
import pos.interfaces.Messages;
import pos.util.IDGenerator;
import pos.util.Validate;


/*
 * A control object class that takes the customer's information form the boundary
 * object and check all customer's information and then store the customer's
 * information into the database.
 * @author wei zhang
 */
public class RegisterNewCustomerControl implements Messages {

    private long customerId;

    /**
     * Accepts the customer registration details and stores it into the repository
     * @param first_name
     * @param last_name
     * @param phone_num
     * @param ad_dress
     * @param postal_code
     * @param e_mail
     * @param ci_ty
     * @param coun_try
     * @param unit
     * @param pass_word
     * @return
     * @throws POSException
     * @throws SQLException 
     */
    public boolean handleRegisterCustomer(String first_name, String last_name,
            String phone_num, String ad_dress,String postal_code, String e_mail,
            String ci_ty, String coun_try, String unit,String pass_word)
    throws POSException, SQLException{

        Validate validate = new Validate();
        IDGenerator newCardNum = new IDGenerator();
        long storeCardId = newCardNum.getID();
        try{
            PreparedStatement stmt = Pos.clerkConn.prepareStatement(SQL.INSERTNEWCUSTOMER);
            first_name = validate.validTextInput(first_name);
            last_name = validate.validTextInput(last_name);
            phone_num = validate.validPhoneNumber(phone_num);
            ad_dress = validate.validAddress(ad_dress);
            postal_code = validate.validPostalCode(postal_code);
            e_mail = validate.validEmail(e_mail);
            ci_ty = validate.validTextInput(ci_ty);
            coun_try = validate.validTextInput(coun_try);
            unit = validate.validUnitNum(unit);
            pass_word = validate.validTextInput(pass_word);
            
            stmt.setString(1,first_name);
            stmt.setString(2,last_name);
            stmt.setLong(3, Long.parseLong(phone_num));
            stmt.setString(4, unit);
            stmt.setString(5, ad_dress);
            stmt.setString(6,postal_code);
            stmt.setString(7,ci_ty);
            stmt.setString(8,coun_try);
            stmt.setString(9,e_mail);
            stmt.setString(10,pass_word);
            stmt.setString(11, String.valueOf(storeCardId));
            stmt.setDate(12, new Date(System.currentTimeMillis()));
            stmt.setDate(13, new Date(System.currentTimeMillis()));
            stmt.setDate(14, new Date(System.currentTimeMillis()));
            stmt.setDate(15, new Date(System.currentTimeMillis()));
            stmt.executeUpdate();
            setStoreId(storeCardId);
            return true;
        }
        catch(POSException e){
            throw new POSException(e.getMessage(), e);

        }
        catch(SQLException e){
            e.printStackTrace();
        }
        catch(NumberFormatException e){
            e.printStackTrace();
        }
        return false;

    }
    /**
     * Set the store card number.
     * @param theCustomerId
     */
    public void setStoreId(Long theCustomerId){
        customerId = theCustomerId;
    }
    /**
     * Get the store card number.
     * @return
     */
    public long getStoreId(){
        return customerId;
    }

}